#include <iostream>

struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if (head == nullptr) {
            return nullptr;
        }
        ListNode new_head(0);
        new_head.next = head;
        ListNode* node = head->next;
        ListNode* pre_node = head;
        while (node != nullptr) {
            pre_node->next = node->next;
            node->next = new_head.next;
            new_head.next = node;
            node = pre_node->next;
        }
        return new_head.next;
    }
};
